import 'package:chathouse/constant/my_color.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'form_widgets.dart';

typedef SelectItemWidget16ItemChange = Function(String? val, bool selectFalse);

//問題類型16
class SelectItemWidget16 extends StatefulWidget {
  //單選
  String title;
  String? selectedValue;
  List<KeyValueBean> initValue;
  SelectItemWidget16ItemChange onSelectedItemChange;
  //否选项
  bool selectFalse;

  bool request;

  SelectItemWidget16(this.title, this.selectedValue, this.initValue,
      this.onSelectedItemChange, this.selectFalse,
      {this.request = false, Key? key})
      : super(key: key);

  @override
  State<SelectItemWidget16> createState() => _SelectItemWidget16State();
}

class _SelectItemWidget16State extends State<SelectItemWidget16> {
  @override
  Widget build(BuildContext context) {
    double itemW = (1.sw - 26.w * 2 - 20.w - 20.w) / 2.0 - 0.1;
    return Container(
        padding: EdgeInsets.only(left: 26.w, right: 26.w),
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Row(crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              widget.request
                  ? Text(
                      "*",
                      style: getTextStyle(18.sp, color: Colors.red),
                    )
                  : Container(),
              Expanded(
                  child: Text(
                widget.title ?? "",
                style: getTextStyle(18.sp, color: Colors.black),
              ))
            ],
          ),
          SizedBox(
            height: 12.w,
          ),
          Container(
            width: double.infinity,
            padding:
                EdgeInsets.only(left: 8.w, right: 8.w, top: 10.w, bottom: 20.w),
            decoration: BoxDecoration(
                border: Border.all(width: 1.w, color: MyColors.gary_158157157),
                borderRadius: BorderRadius.circular(6.w)),
            child: Column(
              children: [
                Text(
                  "是",
                  textAlign: TextAlign.center,
                  style: getTextStyle(16.sp, color: Colors.black),
                ).setContainer(margin: EdgeInsets.only(bottom: 20.w)),
                Wrap(
                  spacing: 20.w,
                  runSpacing: 20.w,
                  children: widget.initValue.map((e) {
                    bool isSelected = widget.selectedValue == e.value;
                    return Container(
                      padding: EdgeInsets.all(6.w),
                      constraints: BoxConstraints(
                          minWidth: itemW, maxWidth: itemW * 2 + 20.w),
                      decoration: BoxDecoration(
                          color: isSelected
                              ? MyColors.green_204243165
                              : Colors.white,
                          border: Border.all(
                              width: 1.w, color: MyColors.gary_158157157),
                          borderRadius: BorderRadius.circular(100.w)),
                      child: Text(
                        e.name,
                        textAlign: TextAlign.center,
                        style: getTextStyle(16.sp, color: Colors.black),
                      ),
                    ).setOnClickListener(() {
                      setState(() {
                        if (widget.selectedValue == e.value) {
                          widget.selectedValue = null;
                        } else {
                          widget.selectedValue = e.value;
                          widget.selectFalse = false;
                        }
                      });
                      widget.onSelectedItemChange(
                          widget.selectedValue, widget.selectFalse);
                    });
                  }).toList(),
                )
              ],
            ),
          ),

          SizedBox(
            height: 36.w,
          ),
          //
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                padding: EdgeInsets.all(6.w),
                constraints:
                    BoxConstraints(minWidth: itemW, maxWidth: itemW * 2 + 20.w),
                decoration: BoxDecoration(
                    color: widget.selectFalse
                        ? MyColors.green_204243165
                        : Colors.white,
                    border:
                        Border.all(width: 1.w, color: MyColors.gary_158157157),
                    borderRadius: BorderRadius.circular(100.w)),
                child: Text(
                  "否",
                  textAlign: TextAlign.center,
                  style: getTextStyle(16.sp, color: Colors.black),
                ),
              ).setOnClickListener(() {
                setState(() {
                  if (widget.selectFalse) {
                    widget.selectFalse = false;
                  } else {
                    widget.selectFalse = true;
                    widget.selectedValue = null;
                  }
                });
                widget.onSelectedItemChange(
                    widget.selectedValue, widget.selectFalse);
              })
            ],
          )
        ]));
  }
}
